<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UserControllerSiam extends MX_Controller {

     private $_template = "siam_iainmataram";

    function __construct()
	{
		parent::__construct();
        $this->access_library_siam->is_logged_in();
        $this->load->model('user_model');
    }

    function checkLoginData()
    {

        $dataLogin = $this->user_model->getDataLogin(strip_quotes($this->input->post('username')));

        if($dataLogin->num_rows()>0){

          $dataLogin = $dataLogin->row();
          if ($dataLogin->PASSWORD==md5($this->security->xss_clean($this->input->post('password')))) {

              $this->browser_check->checkUserAgent();

              $viewKontrolData = $this->system_model->getViewKontrolData($dataLogin->PRODI);

              $sessionData = array(
                            'siamunp_user' => $this->security->xss_clean($this->input->post('username')),
                            'siamunp_user_status' => $dataLogin->STATUS,
                            'siamunp_user_prodi' => $dataLogin->PRODI,
                            'siamunp_user_nama' => $dataLogin->NAMA,
                            'siamunp_user_nim' => $dataLogin->NIM,
                            'siamunp_user_namaprodi' => $dataLogin->NAMA_PRODI,
                            'system_semester' => $viewKontrolData->KODE_SEMESTER,
                            'system_tahun_ajaran' => $viewKontrolData->KODE_TAHUN,
                            'system_status' => $viewKontrolData->KODE_STATUS
                            );
               $this->session->set_userdata($sessionData);
               return TRUE;
           }
           else
               return FALSE;
         }
         else
           return FALSE;
    }

	function login()
	{
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('chk_pwd', '', 'callback_checkLoginData');
        $this->form_validation->set_message('required', 'Error: Masukkan %s');
        $this->form_validation->set_message('checkLoginData', 'Error: Username or Password is incorrect');

        if ($this->form_validation->run($this) == FALSE)
        {
          $this->load->view('login');
        }
        else
          redirect('siam', 'refresh');
	}

    function logout() {
        $data = array(
                 'siamunp_user' => 0,
                 'siamunp_user_status' => 0,
                 'siamunp_user_prodi' => 0,
                 'siamunp_user_nama' => 0,
                 'siamunp_user_nim' => 0,
                 'siamunp_user_namaprodi' => 0,
                 'system_semester' => 0,
                 'system_tahun_ajaran' => 0,
                 'system_status' => 0
				 );

        $this->session->sess_destroy();
        $this->session->unset_userdata($data);
		redirect('siam/login.html', 'location');
	}

    function viewWelcome()
	{
        $data['page'] = 'siam_home';
        $this->load->view($this->_template,$data);
	}
}

